package com.zuipin.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.zuipin.entity.SysMenu;
import com.zuipin.entity.SysRole;
import com.zuipin.framework.emuns.ErrorCode;
import com.zuipin.framework.result.Result;
import com.zuipin.mapper.SysRoleMapper;
import com.zuipin.service.ISysMenuService;
import com.zuipin.service.ISysResourceService;
import com.zuipin.service.ISysRoleMenuService;
import com.zuipin.service.ISysRoleResourceService;
import com.zuipin.service.ISysRoleService;
import com.zuipin.util.DateUtils;
import com.zuipin.util.Pagination;
import com.zuipin.util.RedisUtils;
import com.zuipin.vo.SysRolePermissionVo;

/**
 * 角色服务实现
 * 
 * @ClassName: SysRoleSer5viceImpl
 * @Description: TODO
 * @author  robot
 * @date 2017年09月147日 13:40:12
 */
@Service
public class SysRoleServiceImpl implements ISysRoleService {
	
	@Resource
	private SysRoleMapper			sysRoleMapper;
	@Resource
	private ISysRoleResourceService	sysRoleResourceService;
	@Resource
	private ISysRoleMenuService		sysRoleMenuService;
	@Resource
	private ISysMenuService			sysMenService;
	@Resource
	private ISysResourceService		sysResourceService;
	
	@Override
	public SysRole findById(Long id) {
		return sysRoleMapper.findById(id);
	}
	
	/**
	 * 更新角色   同时更新 角色资源表    角色菜单表
	 * @author zhuangjiajian
	 * @date 2017-9-14 16:49:24
	 */
	@Override
	public void insert(SysRole sysRole) {
		sysRoleMapper.insert(sysRole);
	}
	
	@Override
	public Result<SysRole> update(SysRole sysRole) {
		Result<SysRole> result = new Result<SysRole>();
		sysRole.setUpdateTime(DateUtils.getCurrentDateTime());
		// 如果角色编号为空则补充默认值
		if (com.zuipin.util.StringUtil.isBlank(sysRole.getRoleNo())) {
			String roleNo = SysRole.ROLE_UU_HEAD + RedisUtils.buildSerialNumber(sysRole.getId(), 4);
			sysRole.setRoleNo(roleNo);
		}
		
		try {
			sysRoleMapper.update(sysRole);
		} catch (Exception e) {
			// 违反唯一约束
			if (e.getMessage().indexOf(SysRole.ROLE_NO_INDEX_KEY) >= 0) {
				result.setErrorCode(ErrorCode.OBJECT_EXIST);
				result.setMessage("此编号已经存在,请重新输入");
				result.setCnMessage("此编号已经存在,请重新输入");
				result.setData(null);
				return result;
			} else {
				result.setErrorCode(ErrorCode.DB_EXCEPTION);
				result.setData(null);
				return result;
			}
		}
		
		result.setErrorCode(ErrorCode.SUCCESS);
		return result;
	}
	
	@Override
	public void delete(Long id) {
		sysRoleMapper.delete(id);
		
	}
	
	@Override
	public List<SysRole> seachSysRole(SysRole sysRole, Pagination page) {
		return sysRoleMapper.seachSysRole(sysRole, page);
	}
	
	/**
	 * 改变角色状态
	 * @author zhuangjiajian
	 * @date 2017-9-14 17:51:13
	 * @param status
	 * @return
	 */
	@Override
	public int modifyStatus(Integer status) {
		// TODO Auto-generated method stub
		return sysRoleMapper.modifyStatus(status);
	}
	
	/**
	 * 查询角色下的权限列表信息
	 * @author zhuangjiajian
	 * @date 2017-9-14 17:51:13
	 * @param roleId
	 * @return
	 */
	@Override
	public Result<List<SysRolePermissionVo>> getRolePermissionDetail(Long roleId) {
		
		Result<List<SysRolePermissionVo>> result = new Result<List<SysRolePermissionVo>>();
		List<SysRolePermissionVo> listVo = new ArrayList<SysRolePermissionVo>();
		// 查询 角色菜单表信息 （未被冻结的菜单信息） 显示菜单信息
		List<SysMenu> menuList = sysMenService.findByRoleId(roleId, SysMenu.ENABLE);
		// 查询菜单下的子模块（角色-菜单-资源）
		/*for (SysMenu sysMenu : menuList) {
			SysRolePermissionVo vo = new SysRolePermissionVo();
			vo.setSysId(sysMenu.getSysId());
			List<SysResource> resourcesList = sysResourceService.findByRoleIdAndMenuId(roleId, sysMenu.getId(), sysMenu.getSysId());
			sysMenu.setSysResourceList(resourcesList);
			vo.setSysmenuList(menuList);
		}*/
		result.setData(listVo);
		result.setErrorCode(ErrorCode.SUCCESS);
		return result;
	}
	
	@Override
	public Result<SysRole> addSysRole(SysRole sysRole) {
		Result<SysRole> result = new Result<SysRole>();
		// 设置默认值
		String time = DateUtils.getCurrentDateTime();
		sysRole.setDelFlag(SysRole.DEL_NO);
		sysRole.setRoleStatus(SysRole.ENABLE);
		sysRole.setCreateTime(time);
		sysRole.setUpdateTime(time);
		sysRole.setUpdateId(sysRole.getCreateId());
		
		try {
			sysRoleMapper.addSysRole(sysRole);
		} catch (Exception e) {
			// 违反唯一约束
			if (e.getMessage().indexOf(SysRole.ROLE_NO_INDEX_KEY) >= 0) {
				result.setErrorCode(ErrorCode.OBJECT_EXIST);
				result.setMessage("此编号已经存在,请重新输入");
				result.setCnMessage("此编号已经存在,请重新输入");
				result.setData(null);
				return result;
			} else {
				result.setErrorCode(ErrorCode.DB_EXCEPTION);
				result.setData(null);
				return result;
			}
		}
		// 如果角色编号为空则补充默认值
		if (com.zuipin.util.StringUtil.isBlank(sysRole.getRoleNo())) {
			String roleNo = SysRole.ROLE_UU_HEAD + RedisUtils.buildSerialNumber(sysRole.getId(), 4);
			sysRole.setRoleNo(roleNo);
			sysRoleMapper.update(sysRole);
		}
		result.setData(sysRole);
		result.setErrorCode(ErrorCode.SUCCESS);
		result.setMessage("新增角色成功");
		return result;
	}
	
	@Override
	public String findRoleIdsByUserId(Long userId) {
		return sysRoleMapper.findRoleIdsByUserId(userId);
	}
	
}
